﻿/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A
 * copy of the license can be found at http://go.microsoft.com/fwlink/?LinkID=131993. If
 * you cannot locate the Microsoft Public License, please send an email to
 * mixon@microsoft.com. By using this source code in any fashion, you are agreeing to
 * be bound by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MIXOnline.Descry
{
    /// <summary>
    /// Represents a tree of items to be represented in a TreeMap
    /// </summary>
    /// <remarks>
    /// You can create a collection or tree of TreeMapItems, and pass to LoadTreeMap() to cause the TreeMapControl to render a TreeMap.  
    /// You must specify the size, label, and color for the panel.  You can pass in a generic object to be retrieved from the panel on HitTest,
    /// and you can even specify a custom PanelItemType to force each panel to render your own custom XAML.  PanelItemType must be set to a
    /// type that is a subclass of TreeMapPanelItem, and when the TreeMap is being loaded, panels of your custom type will be created instead of
    /// the base TreeMapPanelItem.
    /// </remarks>
    public class TreeMapItem
    {
        public long Size;
        public string Label;
        public Color Color;
        public List<TreeMapItem> Children;
        public TreeMapItem()
        {
            Children = new List<TreeMapItem>();
        }
        public Type PanelItemType = typeof(TreeMapPanelItem);
        public object Tag;
    }

    /// <summary>
    /// The generic XAML grid that gets rendered in each box of the TreeMap.  You can subclass this if you want to do major customization.
    /// </summary>
    public class TreeMapPanelItem : Grid
    {
        public TreeMapPanelItem() // needs default constructor for Custom PanelItemType support
        {
        }

        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.RegisterAttached(
            "Label",
            typeof(string),
            typeof(TreeMapPanel), null
            );

        private TreeMapItem m_item;

        public TreeMapItem Item
        {
            get { return m_item; }
        }

        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set
            {
                SetValue(LabelProperty, value);
            }
        }

        public static string GetLabel(DependencyObject element)
        {
            if (element == null) throw new NullReferenceException(element.ToString());

            return (string)element.GetValue(LabelProperty);
        }

        public static void SetLabel(DependencyObject element, string value)
        {
            if (element == null) throw new NullReferenceException(element.ToString());

            element.SetValue(LabelProperty, value);
        }

        public TreeMapPanelItem(TreeMapItem item)
        {
            this.m_item = item;
            SetLabel(this, item.Label);
        }
    }
}
